package com.whmskj.xjlhsc.utils;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whmskj.xjlhsc.enums.ScEnums;
import com.whmskj.xjlhsc.enums.ScEnums.SCCLASS;
import com.whmskj.xjlhsc.exception.BusinessException;
import com.whmskj.xjlhsc.jitapi.dto.Emp;
import com.whmskj.xjlhsc.jitapi.mapper.EmpMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @BelongsProject: XJLHSC
 * @BelongsPackage: com.whmskj.xjlhsc.utils
 * @Author: lk
 * @CreateTime: 2025-06-10
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class SCUtil {

    private static final long DEFAULT_CURRENT_PAGE = 1L;
    private static final long DEFAULT_PAGE_SIZE = 10L;

    public static <T> Page<T> getPage() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        return Page.of(NumberUtil.isNumber(request.getParameter("pageNum")) ? Long.parseLong(request.getParameter("pageNum")) : DEFAULT_CURRENT_PAGE, NumberUtil.isNumber(request.getParameter("pageSize")) ? Long.parseLong(request.getParameter("pageSize")) : DEFAULT_PAGE_SIZE);
    }

    /**
     * 获取当班的时间范围
     *
     * @return
     */
    public static ClassTimeRange getClassDateTimeRange() {
        LocalTime now = LocalTime.now();
        if (!now.isBefore(LocalTime.of(9, 30)) && !now.isAfter(LocalTime.of(21, 30))) {
            return new ClassTimeRange(LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.of(9, 30)), LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.of(21, 30)), SCCLASS.AM, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString());
        } else {
            //夜班
            if (now.isBefore(LocalTime.of(21, 30))) {
                return new ClassTimeRange(LocalDateTime.of(LocalDateTime.now().toLocalDate().minusDays(1), LocalTime.of(21, 30)), LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.of(9, 30)), SCCLASS.PM, LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString());

            } else {
                return new ClassTimeRange(LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.of(21, 30)), LocalDateTime.of(LocalDateTime.now().toLocalDate().plusDays(1), LocalTime.of(9, 30)), SCCLASS.PM, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString());
            }
        }
    }

    /**
     * 获取当前登录的员工
     *
     * @return
     */
    public static Emp getCurrentLoginEmp() {
        String userId = StpUtil.getLoginId().toString();
        Emp emp = SpringUtil.getBean(EmpMapper.class).selectEmpByUserId(userId);
        if (ObjectUtil.isNull(emp)) {
            // 记录日志，方便排查问题
            LoggerFactory.getLogger(SCUtil.class).warn("当前登录用户userId={}在员工表中找不到对应的员工信息", userId);
            throw new BusinessException("当前登录用户不存在员工信息，请联系管理员");
        }
        return emp;
    }

    @AllArgsConstructor
    @Data
    public static class ClassTimeRange {
        private LocalDateTime start;
        private LocalDateTime end;
        private SCCLASS scclass;
        private String dateYmd;
    }
}
